package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Patient;
import com.bwie.service.PatientService;
import com.bwie.mapper.PatientMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
* @author 34427
* @description 针对表【t_patient】的数据库操作Service实现
* @createDate 2025-08-21 09:19:53
*/
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>
    implements PatientService{

    @Autowired
    PatientMapper patientMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public R getPatientPageList(PageInfoVo pageInfoVo) {

        Page<Patient> patientPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Patient::getId);
        if(pageInfoVo.getKeyWord() != null){
            wrapper.like(Patient::getPatientName,pageInfoVo.getKeyWord());
        }

        Page<Patient> page = patientMapper.selectPage(patientPage,wrapper);

        return R.OK(page);
    }

    @Override
    public R addPatient(Patient patient) {

        //查询是否存在
        Patient one = patientMapper.selectOne(new LambdaQueryWrapper<Patient>().eq(Patient::getPatientName, patient.getPatientName()));
        if(one != null){
            return R.ERROR(500,"患者已存在");
        }


        patient.setCreateBy("admin");
        patient.setRegistrationStatus(0);
        patient.setKnotType(0);
        patient.setCreateTime(new Date());
        //添加信息
        patientMapper.insert(patient);

        //返回
        return R.OK();
    }

    @Override
    public R updatePatient(Patient patient) {

        patientMapper.updateById(patient);

        return R.OK();
    }
}




